package com.company.huisuo.utils

import android.app.Activity
import java.util.Stack

/**
 * 堆栈管理器
 */
object TaskManager {

    private val stacks: Stack<Activity> = Stack()

    /**
     * 添加Activity到堆栈
     */
    fun Activity?.addTask() {
        this?.also {
            stacks.add(it)
        }
    }

    /**
     * 移除Activity从堆栈
     */
    fun Activity?.removeTask() {
        this?.also {
            stacks.remove(it)
        }
    }

    /**
     * 获取栈顶Activity
     */
    fun getTopActivity(): Activity? {
        return if (stacks.isEmpty()) {
            null
        } else {
            stacks.lastElement()
        }
    }
}